package com.gin.kotlin.no_gay_alliance.database.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.gin.kotlin.no_gay_alliance.database.entity.IpLocationHistory

@Dao
interface IpLocationHistoryDao {
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun add(ipLocationHistory: IpLocationHistory)

    /**
     * 查询指定用户的IP属地历史
     * @param userId Long
     */
    @Query("select * from IpLocationHistory where userId=:userId")
    fun queryByUserId(userId: Long): List<IpLocationHistory>

    /**
     * 按Id查询
     * @param id Array<Long>
     * @return List<IpLocationHistory>
     */
    @Query("select * from IpLocationHistory where id in (:id)")
    fun queryById(id:Array<Long>): List<IpLocationHistory>

    /**
     * 按用户id分组，查询最大ID，用来查询对应的IP属地历史
     * @param userId Array<Long>
     * @return List<Long>
     */
    @Query("select max(id) id from IpLocationHistory where userId in (:userId) group by userId")
    fun queryLastIdByUserId(userId: Array<Long>):List<Long>
}